package com.itheima.service;


import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MemberDao;
import com.itheima.dao.PsychologyDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Psychology;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service(interfaceClass = PsychologyService.class)
@Transactional
public class PsychologyServiceImpl implements PsychologyService {
    @Autowired
    private PsychologyDao psychologyDao;

    @Autowired
    private MemberDao memberDao;

    public PageResult findPage(QueryPageBean pageBean) {
        Integer currentPage = pageBean.getCurrentPage();
        Integer pageSize = pageBean.getPageSize();
        String queryString = pageBean.getQueryString();
        //使用分页助手插件实现分页查询
        PageHelper.startPage(currentPage,pageSize);
        Page<Map<String,Object>> page = psychologyDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void add(Psychology psychology) {
        psychology.setValDate(new Date());
        psychologyDao.add(psychology);
        String name = psychology.getMemberName();
        Integer memberId =memberDao.findByName(name);
        Integer psychologyId = psychology.getId();
        Map<String,Integer> map = new HashMap<>();
        map.put("psychologyId",psychologyId);
        map.put("memberId",memberId);
        psychologyDao.setPsychologyAndMember(map);

    }

    @Override
    public void edit(Psychology psychology) {
        psychologyDao.edit(psychology);
    }

    @Override
    public void delete(Integer id) {
        psychologyDao.deletePhAndMem(id);
        psychologyDao.delete(id);
    }

    @Override
    public Map findById(Integer id) {
        return psychologyDao.findById(id);
    }
}
